<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('项目划款') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('申请标题')" prop="actTitle">
        <el-input
          v-model="queryParams.actTitle"
          :placeholder="$t('申请标题')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('申请人')" prop="applyUser">
        <UserSelect v-model="queryParams.applyUser" :placeholder="$t('申请人')" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['securities:payment:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-hasPermi="['securities:payment:export']"-->
      <!--          type="warning"-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="exportHandle"-->
      <!--        >{{ $t('导出') }}</el-button>-->
      <!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="dealSecuritiesPaymentList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请标题')" align="center" prop="actTitle">
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.actTitle }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请人')" align="center" prop="applyUserName" />
      <el-table-column :label="$t('申请时间')" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('付款金额') + $t('金额单位')" align="center" prop="total">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.total, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('实际付款金额') + $t('金额单位')" align="center" prop="actualPaymentAmount">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.actualPaymentAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('币种')" align="center" prop="currency" :formatter="currencyFormat" />
      <el-table-column :label="$t('审批状态')" align="center" width="120">
        <template slot-scope="scope">
          <ProcessStatus :status="scope.row.actStatus" :act-id="scope.row.actId" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.actStatus === 'W'"
            v-hasPermi="['securities:payment:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updatePayBtn(scope.row)"
          >{{ $t('维护支付结果') }}</el-button>
          <!-- 启动流程，终止流程，修改，删除 四个按钮 -->
          <ListBtn :bus-data="scope.row" has-permi="securities:payment" />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目划款对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealSecuritiesPaymentForm ref="dealSecuritiesPaymentForm" />
    </el-dialog>
    <!-- 维护付款结果 -->
    <el-dialog :title="$t('维护付款结果')" :visible="paymentResultOpen" :close-on-click-modal="false" :lock-scroll="false" append-to-body @close="paymentResultOpen = false">
      <!--      <el-dialog :title="$t('维护支付结果')" :visible.sync="paymentResultOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>-->
      <div v-loading="paymentResultLoading">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
          <el-row>
            <el-col :span="12">
              <el-form-item :label="$t('付款金额')+$t('金额单位')" prop="paymentAmount">
                <Money v-model="form.total" :placeholder="$t('付款金额')" unit="w" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="$t('实际付款金额')+$t('金额单位')" prop="actualPaymentAmount">
                <Money v-model="form.actualPaymentAmount" :placeholder="$t('实际付款金额')" unit="w" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" style="text-align: right;">
        <el-button @click="paymentResultOpen = false">{{ $t("取消") }}</el-button>
        <save-btn has-permi="lease:loanRecord" :function="savePaymentResult" :cancel-show="false" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDealSecuritiesPayment, delDealSecuritiesPayment, saveDealSecuritiesPayment } from '@/api/deal/securities/payment/dealSecuritiesPayment'
import dealSecuritiesPaymentForm from './dealSecuritiesPaymentForm.vue'
export default {
    components: {
        dealSecuritiesPaymentForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          paymentResultLoading: true,
          // 总条数
          total: 0,
          // 项目划款表格数据
          dealSecuritiesPaymentList: [],
          // 币种字典
          currencyOptions: [],
          // 弹出层标题
          title: '项目划款',
          // 是否显示弹出层
          formOpen: false,
          paymentResultOpen: false,
          // 查询参数
          queryParams: {
              dealId: this.$route.query.id,
              actTitle: undefined,
              applyUser: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
            actualPaymentAmount: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
          }
      }
  },
  created() {
      this.getList()
      this.getDicts('currency').then(response => {
        this.currencyOptions = response.data
      })
  },
  methods: {
    /** 查询项目划款列表 */
    getList() {
        this.loading = true
        listDealSecuritiesPayment(this.queryParams).then(response => {
            this.dealSecuritiesPaymentList = response.rows
            this.total = response.total
            this.loading = false
        }).catch(() => {
            this.loading = false
        })
    },
    // 币种字典翻译
    currencyFormat(row) {
      return this.selectDictLabel(this.currencyOptions, row.currency)
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            dealId: undefined,
            actTitle: undefined,
            applyUser: undefined,
            applyTime: undefined,
            total: undefined,
            currency: undefined,
            payeeName: undefined,
            depositBank: undefined,
            accountTitle: undefined,
            bankNumber: undefined,
            addressReceiver: undefined,
            actId: undefined,
            actStatus: '0',
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealSecuritiesPaymentForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealSecuritiesPaymentForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return delDealSecuritiesPayment(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(() => {
          this.loading = false
      })
    },
    // 付款结果维护
    updatePayBtn(row) {
      this.paymentResultOpen = true
      this.paymentResultLoading = false
      this.form = row
    },
    // 保存付款结果维护
    savePaymentResult() {
      this.$refs['form'].validate(valid => {
        // 验证数据
        if (valid) {
          if (this.form.actualPaymentAmount !== this.form.total) {
            this.$confirm(this.$t('与付款金额不一致，是否继续?'), this.$t('警告'), {
              confirmButtonText: this.$t('确定'),
              cancelButtonText: this.$t('取消'),
              type: 'warning'
            }).then(() => {
              this.paymentResultLoading = true
              return saveDealSecuritiesPayment(this.form)
            }).then(response => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('保存成功'))
                this.paymentResultOpen = false
                this.paymentResultLoading = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            }).catch(() => {
              return false
            })
          } else {
            this.paymentResultLoading = true
            saveDealSecuritiesPayment(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('保存成功'))
                this.paymentResultOpen = false
                this.paymentResultLoading = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        } else {
          this.paymentResultLoading = false
          this.locationError(document)
        }
      }).catch(() => {
        this.paymentResultLoading = false
      })
    }
  }
}
</script>
